package com.bifang.core.quartz.gateway.controller;

import cn.hutool.core.convert.Convert;
import cn.hutool.json.JSONUtil;
import com.bifang.common.vo.AppResponse;
import com.bifang.common.vo.BaseVO;
import com.bifang.common.vo.QrtzDetailsVO;
import com.bifang.common.vo.QuartzVO;
import com.bifang.core.quartz.gateway.req.QrtzDetailsReq;
import com.bifang.core.quartz.integration.QuartzIntegration;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 定时任务
 *
 * @author chenzh
 * @date 2019-12-19
 */
@RestController
@RequestMapping(value = "/quartz")
@Api(tags = "定时任务管理")
public class QuartzController {
    @Autowired QuartzIntegration quartzIntegration;
    /** 获取任务列表 */
    @PostMapping(value = "/query/list", produces = "application/json;charset=UTF-8")
    @ApiOperation(value = "获取任务列表", notes = "分页查询获取任务列表")
    public AppResponse<PageInfo<QrtzDetailsVO>> getUserList(
            @RequestBody QrtzDetailsReq qrtzDetailsReq) {
        QrtzDetailsVO qrtzDetailsVO = Convert.convert(QrtzDetailsVO.class, qrtzDetailsReq);
        qrtzDetailsVO.setJobDescription(qrtzDetailsReq.getTitle());
        return AppResponse.successData(quartzIntegration.getQuartzList(qrtzDetailsVO));
    }
    /** 获取任务详情 */
    @ApiOperation(value = "获取任务详情", notes = "获取任务详情")
    @PostMapping(value = "/query/detail", produces = "application/json;charset=UTF-8")
    public AppResponse getDetail(@RequestBody QrtzDetailsReq qrtzDetailsReq) {
        QrtzDetailsVO qrtzDetailsVO = Convert.convert(QrtzDetailsVO.class, qrtzDetailsReq);
        qrtzDetailsVO.setJobDescription(qrtzDetailsReq.getTitle());
        return AppResponse.successData(quartzIntegration.getQuartzDetail(qrtzDetailsVO));
    }
    /** 添加任务 */
    @ApiOperation(value = "添加任务", notes = "添加任务")
    @PostMapping(value = "/addJob", produces = "application/json;charset=UTF-8")
    public AppResponse addJob(@RequestBody QrtzDetailsReq qrtzDetailsReq) {
        QuartzVO quartzVO = Convert.convert(QuartzVO.class, qrtzDetailsReq);
        if (StringUtils.isNotBlank(qrtzDetailsReq.getParameter())) {
            try {
                quartzVO.setParameter(
                        JSONUtil.toBean(
                                JSONUtil.parseObj(qrtzDetailsReq.getParameter()), Map.class));
            } catch (Exception e) {
                return AppResponse.successMsg("任务参数只能是JSONObject格式");
            }
        }
        quartzVO.setJobDescription(qrtzDetailsReq.getTitle());
        quartzVO.setDescription(qrtzDetailsReq.getTitle());
        return AppResponse.successData(quartzIntegration.addJob(quartzVO));
    }

    /** 修改任务 */
    @ApiOperation(value = "修改任务", notes = "修改任务")
    @PostMapping(value = "/updateJob", produces = "application/json;charset=UTF-8")
    public AppResponse updateJob(@RequestBody QrtzDetailsReq qrtzDetailsReq) {
        QuartzVO quartzVO = Convert.convert(QuartzVO.class, qrtzDetailsReq);
        if (StringUtils.isNotBlank(qrtzDetailsReq.getParameter())) {
            try {
                quartzVO.setParameter(
                        JSONUtil.toBean(
                                JSONUtil.parseObj(qrtzDetailsReq.getParameter()), Map.class));
            } catch (Exception e) {
                return AppResponse.successMsg("任务参数只能是JSONObject格式");
            }
        }
        quartzVO.setJobDescription(qrtzDetailsReq.getTitle());
        quartzVO.setDescription(qrtzDetailsReq.getTitle());
        return AppResponse.successData(quartzIntegration.updateJob(quartzVO));
    }

    /** 删除任务 */
    @ApiOperation(value = "删除任务", notes = "删除任务")
    @PostMapping(value = "/deleteJob", produces = "application/json;charset=UTF-8")
    public AppResponse deleteJob(@RequestBody QrtzDetailsReq qrtzDetailsReq) {
        QuartzVO quartzVO = Convert.convert(QuartzVO.class, qrtzDetailsReq);
        quartzVO.setJobDescription(qrtzDetailsReq.getTitle());
        quartzIntegration.deleteJob(quartzVO);
        return AppResponse.success();
    }
    /** 暂停任务 */
    @ApiOperation(value = "暂停任务", notes = "暂停任务")
    @PostMapping(value = "/pauseJob", produces = "application/json;charset=UTF-8")
    public AppResponse pauseJob(@RequestBody QrtzDetailsReq qrtzDetailsReq) {
        QuartzVO quartzVO = Convert.convert(QuartzVO.class, qrtzDetailsReq);
        quartzVO.setJobDescription(qrtzDetailsReq.getTitle());
        return AppResponse.successData(quartzIntegration.pauseJob(quartzVO));
    }
    /** 启用任务 */
    @ApiOperation(value = "启用任务", notes = "启用任务")
    @PostMapping(value = "/resumeJob", produces = "application/json;charset=UTF-8")
    public AppResponse resumeJob(@RequestBody QrtzDetailsReq qrtzDetailsReq) {
        QuartzVO quartzVO = Convert.convert(QuartzVO.class, qrtzDetailsReq);
        quartzVO.setJobDescription(qrtzDetailsReq.getTitle());
        return AppResponse.successData(quartzIntegration.resumeJob(quartzVO));
    }
    /** 暂停所有 */
    @ApiOperation(value = "暂停所有", notes = "暂停所有")
    @PostMapping(value = "/pauseAll", produces = "application/json;charset=UTF-8")
    public AppResponse pauseAll(@RequestBody BaseVO baseVO) {
        quartzIntegration.pauseAll(baseVO);
        return AppResponse.success();
    }
    /** 恢复所有 */
    @ApiOperation(value = "恢复所有", notes = "恢复所有")
    @PostMapping(value = "/resumeAll", produces = "application/json;charset=UTF-8")
    public AppResponse resumeAll(@RequestBody BaseVO baseVO) {
        quartzIntegration.resumeAll(baseVO);
        return AppResponse.success();
    }
    /** 开始所有 */
    @ApiOperation(value = "开始启动所有", notes = "开始启动所有")
    @PostMapping(value = "/startJobs", produces = "application/json;charset=UTF-8")
    public AppResponse startJobs(@RequestBody BaseVO baseVO) {
        quartzIntegration.startJobs(baseVO);
        return AppResponse.success();
    }
    /** 停止所有 */
    @ApiOperation(value = "停止所有", notes = "停止所有")
    @PostMapping(value = "/shutdownJobs", produces = "application/json;charset=UTF-8")
    public AppResponse shutdownJobs(@RequestBody BaseVO baseVO) {
        quartzIntegration.shutdownJobs(baseVO);
        return AppResponse.success();
    }

    /** 重置错误状态任务 */
    @ApiOperation(value = "重置错误状态任务", notes = "重置错误状态任务")
    @PostMapping(value = "/resetTriggerFromErrorState", produces = "application/json;charset=UTF-8")
    public AppResponse resetTriggerFromErrorState(@RequestBody QrtzDetailsReq qrtzDetailsReq) {
        QuartzVO quartzVO = Convert.convert(QuartzVO.class, qrtzDetailsReq);
        quartzVO.setJobDescription(qrtzDetailsReq.getTitle());

        return AppResponse.successData(quartzIntegration.resetTriggerFromErrorState(quartzVO));
    }

    /** 立即执行一次任务 */
    @ApiOperation(value = "立即执行一次任务", notes = "立即执行一次任务")
    @PostMapping(value = "/triggerJob", produces = "application/json;charset=UTF-8")
    public AppResponse triggerJob(@RequestBody QrtzDetailsReq qrtzDetailsReq) {
        QuartzVO quartzVO = Convert.convert(QuartzVO.class, qrtzDetailsReq);
        quartzVO.setJobDescription(qrtzDetailsReq.getTitle());

        return AppResponse.successData(quartzIntegration.triggerJob(quartzVO));
    }
}
